
import axios from 'axios'
// import { Message } from 'element-ui'
import { Toast } from 'vant'
import { ServerError500, ServerError403, RequestError } from '../Error'
import cookie from '@/lib/cookie'
import Vue from 'vue'
const _this = new Vue()
let userInfo = cookie.readCookie('userInfo') ? JSON.parse(cookie.readCookie('userInfo')) : '';
export default class Http {
  BASEURL = process.env.VUE_APP_BASE_URL
  IMGURL = process.env.VUE_APP_BASR_IMG_URL
  constructor() { }
  async get(ctx) {
    try {
      ctx.url = this.BASEURL + ctx.url
      //get的请求的参数用params接受
      ctx.params = ctx.data
      delete ctx.data
      ctx.method = 'get'
      ctx.headers = {
        Accept: 'application/json',
        Authorization: userInfo ? userInfo.userCode : '',
        ...ctx.headers,
      }
      ctx.timeout = process.env.VUE_APP_ROUTER_TIME
      ctx.before = ctx.before || ((ctx) => ctx) //局部请求拦截器
      ctx.after = ctx.after || ((ctx) => ctx) //局部响应拦截器
      ctx.errorCatch = ctx.errorCatch || ((err) => err) //局部异常拦截器拦截
      ctx = this.globalCatchRequest(ctx) //请求前通过全局请求拦截器
      ctx = ctx.before(ctx)
      let res = await axios.request(ctx)
      let resByGlo = this.globalCatch(res) //全局拦截后的res
      let resFinal = ctx.after(resByGlo)
      return resFinal
    } catch (err) {
      let errBylocal = ctx.errorCatch(err)
      //局部拦截后的res
      let errByGlo = this.globalErrorCatch(errBylocal)
      return Promise.reject(errByGlo)
      //全局拦截后的res;
    }
  }
  async post(ctx) {
    try {
      ctx.url = this.BASEURL + ctx.url
      ctx.method = 'POST'

      ctx.headers = {
        Accept: 'application/json',
        Authorization: userInfo ? userInfo.userCode : '',
        ...ctx.headers,
      }
      ctx.params = ctx.data
      delete ctx.data
      ctx.timeout = process.env.VUE_APP_ROUTER_TIME
      ctx.before = ctx.before || ((ctx) => ctx) //局部请求拦截器
      ctx.after = ctx.after || ((ctx) => ctx) //局部响应拦截器
      ctx.errorCatch = ctx.errorCatch || ((err) => err) //局部异常拦截器拦截
      ctx = this.globalCatchRequest(ctx) //请求前通过全局请求拦截器
      ctx = ctx.before(ctx)
      let res = await axios.request(ctx)
      let resByGlo = this.globalCatch(res) //全局拦截后的res
      let resFinal = ctx.after(resByGlo)
      return resFinal
    } catch (err) {
      let errBylocal = ctx.errorCatch(err)
      //局部拦截后的res
      let errByGlo = this.globalErrorCatch(errBylocal)
      return Promise.reject(errByGlo)
      //全局拦截后的res;
    }
  }
  globalErrorCatch(err) {
    // window.loading.close();
    console.warn(err)
    // throw new RequestError(err)
    return err
  }
  globalCatch(res) {
    // window.loading.close();
    let code = res.data.code
    let message = res.data.message || res.data.content
    switch (code) {
      case 200:
        return res
      // break;
      case 500:
        Toast(message)
        throw new Error(message)
      // break;
      case 403:
        Toast(message)
        throw new Error(message)
      case 10501:
        Toast(message)
        throw new Error(message)
      case 400:
        Toast(message)
        throw new Error(message)
      case 10400:
        MessageBox.alert(message, '提示', {
          confirmButtonText: '去登录',
          type: 'warning',
          center: true,
        }).then(() => {
          window.open('http://h5.cbda1984.com/login')
        })
        break;
      default:
        break
    }

    return res
  }
  globalCatchRequest(ctx) {
    //  window.loading = _this.$loading();
    // setTimeout(() => {
    //     loading.close();
    // }, 2000);
    //对请求前的内容处理，返回处理后的内容
    let token = localStorage.getItem('APP_TOKEN') || ''
    if (token) {
      ctx.headers['token'] = token
    }
    //请求拦截器
    return ctx
  }
  setImg(listOrObj, key) {
    //改变元数据
    if (Object.prototype.toString.call(listOrObj) === '[object Object]') {
      listOrObj['_' + key] = this.IMGURL + listOrObj[key]
    }
    if (Object.prototype.toString.call(listOrObj) === '[object Array]') {
      listOrObj.map((val) => {
        val['_' + key] = this.IMGURL + val[key]
      })
    }
  }
}
